/**
 * 
 */
package com.pactera.android.activity;

import com.pactera.android.R;
import com.pactera.android.application.PacteraApplication;
import com.pactera.android.service.NotificationService;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

/**
 * @author David Zheng
 *
 */
public abstract class BaseActivity extends Activity {
	
	PacteraApplication app;
	
	protected String errorHtml = "<html><body style='text-align:center;'><h1>Page not found error!!!</h1></body></html>";
			
	/**
	 * init the buttons and webview
	 */
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		app = (PacteraApplication)getApplication();		
	}
	
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.base_menu, menu);
		return true;
	}

	
	/* (non-Javadoc)
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		 switch (item.getItemId()) {
	        case R.id.refresh:	            
	            return true;	            
	        case R.id.quit:
	        	Intent intent = new Intent(Intent.ACTION_MAIN);
	        	intent.addCategory(Intent.CATEGORY_HOME);
	        	intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	        	startActivity(intent);
	            return true;
	        case R.id.toggleService:
	        	if (app.isNotificationServiceRunning()) {
	        		stopService(new Intent(this, NotificationService.class));
	        	} else {
	        		startService(new Intent(this, NotificationService.class));
	        	}
	        	return true;
	        default:
	            return super.onOptionsItemSelected(item);
		 }
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onMenuOpened(int, android.view.Menu)
	 */
	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {		
		MenuItem toggleItem = menu.findItem(R.id.toggleService);		
		
		if (app.isNotificationServiceRunning()) {
			toggleItem.setTitle(R.string.stop);
			toggleItem.setIcon(R.drawable.ic_menu_close_clear_cancel);
		} else {
			toggleItem.setTitle(R.string.start);
			toggleItem.setIcon(R.drawable.ic_menu_refresh);			
		}
		
		return true;
	}
	
	

}
